package com.lakeworks.web.controller.core;

import com.lakeworks.common.annotation.EnumValue;
import com.lakeworks.common.annotation.RepeatSubmit;
import com.lakeworks.common.core.controller.BaseController;
import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.domain.CompleteInfo;
import com.lakeworks.common.domain.system.OprDefeimg;
import com.lakeworks.common.domain.system.OprProductImg;
import com.lakeworks.common.enums.BoxCodeType;
import com.lakeworks.common.enums.OprProductType;
import com.lakeworks.domain.OprMerchandise;
import com.lakeworks.domain.OprShipment;
import com.lakeworks.domain.vo.ClothInfo;
import com.lakeworks.domain.vo.PackageQueryPara;
import com.lakeworks.domain.vo.ProductVo;
import com.lakeworks.service.IOprAttachmentService;
import com.lakeworks.service.IOprProductService;
import com.lakeworks.service.IOprShipmentService;
import com.lakeworks.service.api.MerchantAsync;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

import static java.util.Arrays.stream;

/**
 * 作业产品Controller
 *
 * @author jf
 * @date 2023-02-08
 */
@RestController
@RequestMapping("/product")
@Validated
public class OprProductController extends BaseController {
    @Autowired
    private IOprProductService oprProductService;

    @Autowired
    private IOprShipmentService oprShipmentService;

    @Autowired
    private MerchantAsync merchantAsync;

    @Autowired
    private IOprAttachmentService oprAttachmentService;

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 作业产品 添加附件
     *
     * @param id    产品id
     * @param attId 系统附件id
     */
    @PostMapping("/{id}/attachment/{attId}")
    public AjaxResult addProAttachment(@PathVariable Long id, @PathVariable Long attId,@RequestBody String complete) {
        return oprProductService.addProAttachment(id, attId,complete);
    }

    /**
     * 作业产品 删除附件
     *
     * @param id    产品id
     * @param attId 附件id（非系统附件）
     */
    @DeleteMapping("/{id}/attachment/{attId}")
    public AjaxResult delProAttachment(@PathVariable Long id, @PathVariable Long attId) {
        return oprProductService.delProAttachment(id, attId);
    }

    /**
     * 总检-配附件
     *
     * @param barcode 产品水洗码
     */
    @GetMapping("/check/{barcode}/attachments")
    public AjaxResult attsOfProduct(@PathVariable String barcode) {

        return oprProductService.attsOfProduct(barcode);
    }

    /**
     * 附件扫描
     *
     * @param proBarcode 产品水洗码
     * @param attBarcode 附件水洗码
     */
    @PostMapping("/{proBarcode}/attachment/{attBarcode}/scan")
    public AjaxResult attachmentScan(@PathVariable String proBarcode, @PathVariable String attBarcode) {
        return oprProductService.attachmentScan(proBarcode, attBarcode);
    }

    /**
     * 包装
     * 包装同一个出厂单的第一个衣物时 分配格架
     *
     * @param barcode
     */
    @GetMapping("/wrap/{barcode}")
    public AjaxResult wrap(@PathVariable String barcode) {
        return oprProductService.wrap(barcode);
    }

    /**
     * 打印总检标  status -> PLACED（已包装）
     */
    @GetMapping("/checkLable/{barcode}")
    public AjaxResult printCheckLable(@PathVariable String barcode) {
        return oprProductService.printCheckLable(barcode);
    }

    /**
     * 衣物上架  同一个出厂单全部衣物上架完  请求下物流单
     */
    @PutMapping("/putaway/{barcode}")
    public AjaxResult putaway(@PathVariable String barcode) {
        return oprProductService.putaway(barcode, true);
    }


    @PostMapping("/package/{status}/clothes")
    public AjaxResult packageClothes(@PathVariable @EnumValue(strValues = {"1","2"}, message = "打包状态输入有误") String status,
                                     @RequestBody PackageQueryPara para) {
        return oprProductService.clothes(status,para);
    }

    @PostMapping("/package/{status}/shipments")
    public AjaxResult packageShipments(@PathVariable @EnumValue(strValues = {"1","2"}, message = "打包状态输入有误") String status,
                                       @RequestBody PackageQueryPara para) {
        return oprProductService.shipments(status,para);
    }

    /**
     * 校验盒子吗
     */
    @PostMapping("/box/{boxCode}/check")
    public AjaxResult checkBox(@NotBlank(message = "盒子码错误") @PathVariable String boxCode) {
        List<String> list = stream(BoxCodeType.values()).map(Enum::toString).toList();
        if (!list.contains(boxCode)) {
            return error("无效的盒子码");
        }
        return success();
    }

    /**
     * 打包盒子校验
     */
    @PostMapping("/package/{boxCode}/check")
    public AjaxResult boxCodeCheck(@PathVariable String boxCode,String barcode) {
        return oprProductService.boxCheck(boxCode, barcode);
    }

    /**
     * 打包---取消出厂单
     */
    @PostMapping("/package/{shipmentNo}/cancel")
    public AjaxResult cancelShipment(@PathVariable String shipmentNo) {
        return oprProductService.cancelShipment(shipmentNo);
    }

    /**
     * 打包详情
     *
     * @param barcode 产品水洗码
     */
    @GetMapping("/package/{barcode}/detail")
    public AjaxResult packageDetail(@PathVariable String barcode) {
        return oprProductService.packageDetail(barcode);

    }

    /**
     * 根据出厂单id获取出厂单产品详情
     */
    @GetMapping("/shipment/{id}/products")
    public AjaxResult getShipmentById(@PathVariable Long id) {
        OprShipment oprShipment = oprShipmentService.selectOprShipmentById(id);
        if (oprShipment == null)
            return error("出厂单不存在");

        return success(oprProductService.packageDetail(id));
    }

    /**
     * 产品打包扫描  出厂单中产品全部扫描完 返回面单图片地址
     */
    @PostMapping("/package/{barcode}/scan")
    public AjaxResult packageScan(@PathVariable String barcode) {

        return oprProductService.packageScan(barcode);
    }

    /**
     * 面单扫描  扫完快递面单  打包才算完成
     */
    @PostMapping("/label/{expressNo}/scan")
    public AjaxResult labelScan(@PathVariable String expressNo) {

        return oprProductService.labelScan(expressNo);
    }

    /**
     * 出厂
     *
     * @param expressNo 运单号
     */
    @PostMapping("/outFactory/{expressNo}")
    public AjaxResult outFactory(@PathVariable String expressNo) {

        return oprProductService.outFactory(expressNo);
    }

    /**
     * 衣物核查--查询商品列表
     *
     * @param expressNo
     */
    @GetMapping("/merchandises")
    public AjaxResult getProductList(@NotBlank(message = "运单号不能为空") String expressNo) {
        return oprProductService.getProductList(expressNo);
    }

    /**
     * 衣物核查--查询附件列表
     *
     * @param
     */
    @GetMapping("{oprProductId}/attachments")
    public AjaxResult getAttachmentList(@NotNull(message = "参数错误") @PathVariable("oprProductId") Long oprProductId) {
        return AjaxResult.success(oprProductService.getAttachmentList(oprProductId));
    }


    /**
     * 衣物核查--查询瑕疵列表
     *
     * @param
     */
    @GetMapping("{oprProductId}/flaws")
    public AjaxResult getFlawsList(@NotNull(message = "参数错误") @PathVariable("oprProductId") Long oprProductId) {
        return oprProductService.getFlawsList(oprProductId);
    }


    /**
     * 衣物核查--查询产品图片列表
     *
     * @param
     */
    @GetMapping("{oprProductId}/imgs")
    public AjaxResult getOprProductImgList(@NotNull(message = "参数错误") @PathVariable("oprProductId") Long oprProductId, @NotBlank(message = "参数错误") String imgType) {
        return AjaxResult.success(oprProductService.getOprProductImgList(oprProductId, imgType));
    }


    /**
     * 衣物核查--添加作业产品信息
     *
     * @param productVo
     */
    @PostMapping("/oprProduct")
    public AjaxResult addOprProduct(@Valid @RequestBody ProductVo productVo) {
        return oprProductService.addOprProduct(productVo);
    }
    /**
     * 衣物核查--删除作业产品信息
     *
     * @param
     */
    @DeleteMapping("/oprProduct/{id}")
    public AjaxResult delOprProduct(@PathVariable("id")Long id){
        return oprProductService.delOprProduct(id);
    }


    /**
     * 衣物核查--添加商品
     *
     * @param oprMerchandise
     */
    @PostMapping("/merchandise")
    public AjaxResult addMerchandise(@Valid @RequestBody OprMerchandise oprMerchandise) {
        return oprProductService.addMerchandise(oprMerchandise);
    }



    /**
     * 衣物核查--添加作业产品衣物类型
     *
     * @param flag id
     */
    @PostMapping("{id}/type")
    public AjaxResult addProductType(@NotNull(message = "flag不能为空") Integer flag, @NotNull(message = "id不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(oprProductService.addProductType(flag, id));
    }

    /**
     * 衣物核查--删除作业产品衣物类型
     *
     * @param flag id
     */
    @DeleteMapping("{id}/type")
    public AjaxResult deleteProductType(@NotNull(message = "flag不能为空") Integer flag, @NotNull(message = "id不能为空") @PathVariable("id") Long id) {
        return AjaxResult.success(oprProductService.deleteProductType(flag, id));
    }


    /**
     * 衣物核查--添加作业产品图片信息
     *
     * @param oprProductImg
     */
    @PostMapping("/img")
    public AjaxResult addImg(@Valid @RequestBody OprProductImg oprProductImg) {
        return oprProductService.addImg(oprProductImg);
    }


    /**
     * 衣物核查--添加图片风险提报说明
     *
     * @param
     */
    @PostMapping("/img/{imgId}/desc")
    public AjaxResult addImgDesc(@NotNull(message = "参数错误") @PathVariable("imgId") Long imgId, @NotBlank(message = "参数错误") @RequestBody String remark) {
        return oprProductService.addImgDesc(imgId, remark);
    }


    /**
     * 衣物核查--添加图片瑕疵信息
     *
     * @param oprDefeimg
     */
    @PostMapping("/img/{id}/flaw")
    public AjaxResult addDefeImg(@Valid @RequestBody OprDefeimg oprDefeimg) {
        return oprProductService.addDefeImg(oprDefeimg);
    }

    /**
     * 衣物核查--删除作业产品图片
     *
     * @param imgId
     */
    @DeleteMapping("/img/{imgId}")
    public AjaxResult delImg(@NotNull(message = "参数错误") @PathVariable("imgId") Long imgId) {
        return AjaxResult.success(oprProductService.delImg(imgId));
    }

    /**
     * 衣物核查--删除瑕疵图片
     *
     * @param id
     */
    @DeleteMapping("/img/flaw/{id}")
    public AjaxResult delDefeImg(@NotNull(message = "参数错误") @PathVariable("id") Long id) {
        return AjaxResult.success(oprProductService.delDefeImg(id));
    }

    /**
     * 衣物核查--入厂核查
     *
     * @param oprProductIds
     * @param pickId
     */
    @RepeatSubmit
    @PostMapping("{pickId}/check")
    public AjaxResult check(@NotNull(message = "参数错误") Long[] oprProductIds, @NotNull(message = "参数错误") @PathVariable("pickId") Long pickId) {
        return oprProductService.check(oprProductIds, pickId);
    }


    /**
     * 质检---查询产品详情
     *
     * @param barcode
     */
    @GetMapping("/{barcode}")
    public AjaxResult getProductByBarcode(@PathVariable("barcode") @NotBlank(message = "水洗码不能为空") String barcode) {
        return oprProductService.getProductByBarcode(barcode);
    }

    /**
     * 质检--质检合格
     *
     * @param barcode
     */
    @PostMapping("{barcode}/qualified")
    public AjaxResult qualified(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.qualified(barcode, null);
    }


    /**
     * 质检--配对产品同时质检合格
     *
     * @param barcode
     */
    @PostMapping("{barcode}/{pairbarcode}/qualified")
    public AjaxResult qualified(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode,
                                @NotBlank(message = "配对水洗码不能为空") @PathVariable("pairbarcode") String pairbarcode) {
        return oprProductService.pairQualified(barcode, pairbarcode);
    }


    /**
     * 质检--污渍终止
     *
     * @param barcode
     */
    @PostMapping("{barcode}/stain")
    public AjaxResult stain(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode, String pairbarcode) {
        return oprProductService.stain(barcode, pairbarcode);
    }


    /**
     * 质检--反洗
     *
     * @param barcode
     */
    @PostMapping("{barcode}/rewash")
    public AjaxResult rewash(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode, String comment) {
        return oprProductService.rewash(barcode, comment);
    }


    /**
     * 质检--丢附件
     *
     * @param
     */
 /*   @PostMapping("{barcode}/attachments/lose")
    public AjaxResult loseAttachment(@PathVariable("barcode") String barcode,List<String> names,String remark){
        return oprProductService.loseAttachment(barcode,names,remark);
    }*/


    /**
     * 质检--在厂售后
     *
     * @param
     */
  /*  @PostMapping("{barcode}/afterSale")
    public AjaxResult afterSale(@PathVariable("barcode") String barcode,List<String> imgs,String remark){
        return oprProductService.afterSale(barcode,imgs,remark);
    }*/


    /**
     * S3上传图片
     *
     * @param
     */
    @GetMapping("s3/key")
    public String getS3Code() {
        return oprProductService.getUploadCredential().toString();
    }


    /**
     * 作业产品进洗涤车间
     *
     * @param
     */
    @PostMapping("{barcode}/washing")
    public AjaxResult washing(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.washing(barcode, true);
    }


    /**
     * 作业产品出洗涤车间
     *
     * @param
     */
    @PostMapping("{barcode}/washed")
    public AjaxResult washed(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.washed(barcode, true);
    }


    /**
     * 作业产品待质检(进行粘毛去球后处理)
     *
     * @param
     */
    @PostMapping("{barcode}/toinspect")
    public AjaxResult toinspect(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.toinspect(barcode, true);
    }


    /**
     * 作业产品拣货
     *
     * @param
     */
    @PostMapping("{barcode}/topick")
    public AjaxResult topick(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.topick(barcode, true);
    }


    /**
     * 作业产品已上架
     *
     * @param
     */
    @PostMapping("{barcode}/onshelf")
    public AjaxResult onshelf(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return AjaxResult.success(oprProductService.updateStatueByBarcode(barcode, OprProductType.PLACED.getValue()));
    }


    /**
     * 工单系统通过水洗码获取订单信息
     *
     * @param
     */
    @GetMapping("{barcode}/orderinfo")
    public AjaxResult getOrderInfo(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode) {
        return oprProductService.getOrderInfo(barcode);
    }

    /**
     * 入厂衣物列表
     * @param
     */
    @GetMapping("oprProducts")
    public AjaxResult getOprProducts(ClothInfo clothInfo){
       return success(oprProductService.getOprProducts(clothInfo));
    }


    /**
     * 根据水洗吗获取洗衣流程
     * @param
     */
    @GetMapping("/{barcode}/record")
    public AjaxResult getBarcodeRecord(@PathVariable("barcode") String barcode){
        return oprProductService.getBarcodeRecord(barcode);
    }


    /**
     * 出厂衣物统计列表
     * @param
     */
    @GetMapping("/shipped/cloths")
    public AjaxResult leaveFactory(@NotNull(message = "渠道ID不能为空") Long channelId,
                                   @NotBlank(message = "洗涤方式不能为空") String washMode,
                                   String startTime,String endTime){
        return oprProductService.leaveFactory(channelId,washMode,startTime,endTime);
    }


    /**
     * 补拍，补附件衣物信息查询
     * @param
     */
    @GetMapping("/complete/{barcode}")
    public AjaxResult completeCloth(@NotBlank(message = "水洗码不能为空") @PathVariable("barcode") String barcode){
        return oprProductService.completeCloth(barcode);
    }

    /**
     * 补拍，补附件提交
     * @param
     */
    @PostMapping ("/complete")
    public AjaxResult complete(@Valid @RequestBody CompleteInfo completeInfo){
        return oprProductService.complete(completeInfo);
    }


}
